package rabbitmq;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.MessageProperties;

import io.netty.handler.timeout.TimeoutException;

public class Producer {
    public final static String QUEUE_NAME="rabbitMQ.test";

    public static void main(String[] args) throws IOException, TimeoutException, java.util.concurrent.TimeoutException {
        //创建连接工厂
        ConnectionFactory factory = new ConnectionFactory();
        //设置RabbitMQ相关信息
//        factory.setHost("192.168.69.61");
//        factory.setPort(5672);
//        factory.setUsername("hcyy");
//        factory.setPassword("hcyy@2018");  
        
        factory.setHost("localhost");
        factory.setPort(5672);
        factory.setUsername("guest");
        factory.setPassword("guest");  
        //创建一个新的连接
        Connection connection = factory.newConnection();
        //创建一个通道
        Channel channel = connection.createChannel();
        channel.queueDeclare(QUEUE_NAME, true, false, false, null);
        //  声明一个队列        channel.queueDeclare(QUEUE_NAME, false, false, false, null);
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        
        Date date1 = new Date();
        long from = date1.getTime();
        long i = 0;
        while(true){
        	Date date2 = new Date();
            long to = date2.getTime();
            if((to-from)>1*1) {
            	break;
            }
        	String message = df.format(date2) + "  Hello RabbitMQ:湖南长沙";
        	//发送消息到队列中
            channel.basicPublish("", QUEUE_NAME, MessageProperties.MINIMAL_PERSISTENT_BASIC, message.getBytes("UTF-8"));
            System.out.println("Producer Send +'" + message + "'");
            i++;
        }
        System.out.println("Producer Send count:" + i);
        //关闭通道和连接
        channel.close();
        connection.close();
    }
}
